package cn.izeno.wx.service

import cn.izeno.wx.models.component.AppToken
import cn.izeno.wx.models.miniprogram.MiniProgramApp
import cn.izeno.wx.models.miniprogram.Session
import io.reactivex.rxjava3.core.Observable
import retrofit2.http.GET
import retrofit2.http.Query

interface MiniProgramService {
  /**
   * [auth.code2Session](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html)
   */
  @GET("https://api.weixin.qq.com/sns/jscode2session")
  fun codeToSession(
      @Query("js_code") code: String,
      @Query("appid") appid: String = MiniProgramApp.appid,
      @Query("secret") secret: String = MiniProgramApp.secret,
      @Query("grant_type") type: String = "authorization_code"
  ): Observable<Session>

  /**
   * # [auth.getAccessToken](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html)
   * 获取小程序全局唯一后台接口调用凭据（access_token）。调调用绝大多数后台接口时都需使用 access_token，开发者需要进行妥善保存。
   */
  @GET("https://api.weixin.qq.com/cgi-bin/token")
  fun accessToken(
      @Query("appid") appid: String = MiniProgramApp.appid,
      @Query("secret") secret: String = MiniProgramApp.secret,
      @Query("grant_type") grantType: String = "client_credential"
  ): Observable<AppToken>
}
